package controller.server;

//import java.io.BufferedReader;
//import java.io.InputStreamReader;
//import java.io.PrintWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import model.server.*;
import model.game2048solver.*;
/**

 * @author Peled Eldan
 * @author Daniel Manor
 */

public class RunServerTwo {

	public static void main(String[] args) throws Exception{
		ServerTwo s = new ServerTwo(5000, 10, new ClientHandler() {
		Game2048Solver mySolver = new Game2048Solver();
			@Override
			public void handleClient(ObjectInputStream inFromClient, ObjectOutputStream out2client) {
				
				try {
					while (true) {
						Message messageIn = (Message) inFromClient.readObject();
						System.out.println("Message from client: " + messageIn.getMsg());
						if (messageIn.getMsg().equals("exit")) {
							System.out.println("Client says: I'm out of here");
							break;
						} else if (messageIn.getMsg().equals("getHint") && messageIn.getGame().equals("2048")) {
							
							mySolver.findBestMove(messageIn.getState().getBoard());
							int direction = mySolver.getBestMove();
							out2client.writeObject(new Message(null, "This is the best next move", direction, messageIn.getGame()));
						}
					}
				}catch (IOException e) {
					e.printStackTrace();
				}
				catch (ClassNotFoundException e) {
				e.printStackTrace();
				} 
			}
		});

		s.start();
		
		Thread.sleep(5000);
		
		//s.close();
	}

}
